<?php

class GoodModel extends Model{
	
	public $table='good';
	
	/*public $validate=array(
		array('gname','nonull','商品名称不能为空',2,3),
		array('price','nonull','商城价格不能为空',2,3),
		
	);*/
	
	public $auto=array(
		array('stime','_stime','method',2,3),
		array('tid','_tid','method',2,3),
			
	);
	
	
	public function _tid(){
		
		$data=K('Category')->get_tid(array('cid'=>Q('post.cid')));
		return $data[0];
		
	}
	
	public function _stime(){
		
		return time();
	}
	
	
	public function get_all_data($where=null){
		
		return $this->where($where)->all();
	}	
	
	public function add_data(){
		
		if(!$this->create()) return FALSE;
		

//		添加商品表
//		$this->add();
		$gid= $this->add();
		
//		添加商品详细表
		$sthumb=Q('post.sthumb');
		$mthumb=Q('post.mthumb');
		
		$data=array(
			'gdetails'=>$_POST['editorValue'],
			'gid'=>$gid,
		);
		if(isset($sthumb) && isset($mthumb)){
				$data['sthumb']=implode(',', $sthumb);
				$data['mthumb']=implode(',', $mthumb);
				
		}
	    K('gdata')->add_data($data);
		
//		商品属性表
		$arr=array();
		$gpvalue= Q('post.gpvalue');
		$aprice=Q('post.aprice');
		$tpid=Q('post.tpid');
		if(isset($tpid)){
			
			foreach ($tpid as $k => $v) {
				$arr=array(
					'gid'=>$gid,
					'tpid'=>$v,
					'gpvalue'=>$gpvalue[$k],
					'aprice'=>$aprice[$k],
				);
				K('gproperty')->add_data($arr);
			}
		}
//		p(Q('post.'));die;
		return TRUE;
	}

	public function get_one_data($where=null){
		
		return $this->where($where)->find();
	}
	
	public function edit_data($where){
		
		if(!$this->create()) return FALSE;
		
//		修改商品表
		$this->where($where)->update();
//		修改商品详细表

		$where=array('gid'=>Q('get.gid'));
		$data=array(
			'gdetails'=>$_POST['editorValue'],
			'gid'=>Q('get.gid'),
		);
		
		$sthumb=Q('post.sthumb');
		$mthumb=Q('post.mthumb');
		
		if(isset($sthumb) && isset($mthumb)){
				$data['sthumb']=implode(',', $sthumb);
				$data['mthumb']=implode(',', $mthumb);
				
		}
	    K('gdata')->update_data($where,$data);
		
		
//		修改商品属性表
			K('gproperty')->update_data($where);
		return TRUE;
	}
	
	public function del_data($where=null){
		
//		删除商品表
		$this->where($where)->delete();
		
//		p($where);die;
//		删除商品详细表
		K('gdata')->del_data($where);
//		删除商品属性表
		K('gproperty')->del_data($where);
		
	}
	
	
}







